﻿namespace QMVnSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CDKTDataAccess
    {
        public bool Delete(object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", po_KeyCode), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.Output;
            values[1].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdKT0_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public DataTable GetDataTable(string ps_Subsidiary)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdKT0_Tk_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public DataTable GetDataTable(string ps_Subsidiary, object ps_Code)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Tk", ps_Code) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdKT0_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public int Save(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", row["Ma_Dvcs"]), new SqlParameter("p_Ngay_Ct", row["Ngay_Ct"]), new SqlParameter("p_So_Ct", row["So_Ct"]), new SqlParameter("p_Ma_Bp", row["Ma_Bp"]), new SqlParameter("p_Ma_Hd", row["Ma_Hd"]), new SqlParameter("p_Ma_Dt", row["Ma_Dt"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Ma_Tte", row["Ma_Tte"]), new SqlParameter("p_Ty_Gia", row["Ty_Gia"]), new SqlParameter("p_Ty_Gia_Ht", row["Ty_Gia_Ht"]), new SqlParameter("p_Tk", row["Tk"]), new SqlParameter("p_Du_No_Nt", row["Du_No_Nt"]), new SqlParameter("p_Du_No", row["Du_No"]), 
                new SqlParameter("p_Du_Co_Nt", row["Du_Co_Nt"]), new SqlParameter("p_Du_Co", row["Du_Co"]), new SqlParameter("p_Du_No_Nt0", row["Du_No_Nt0"]), new SqlParameter("p_Du_No0", row["Du_No0"]), new SqlParameter("p_Du_Co_Nt0", row["Du_Co_Nt0"]), new SqlParameter("p_Du_Co0", row["Du_Co0"]), new SqlParameter("p_Han_Tt", row["Han_Tt"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x17].Direction = ParameterDirection.Output;
            values[0x17].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdKT0_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[0x17].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }
    }
}

